package com.hc.sort.manager;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.hc.sort.server.pojo.*;
import com.hc.sort.server.service.*;
import java.util.List;
import java.awt.event.*;

public class BusinessWindowAlterDialog extends JDialog {
	// 持有父窗口的实例，在保存成功的时候刷新表格数据
	static ManagerUI managerUI;
	static int serviceWindowId;
	// 业务层对象
	BusinessWindowService windowService = new BusinessWindowServiceImpl();
	BusinessTypeService typeServer = new BusinessTypeServiceImpl();

	private final JPanel contentPanel = new JPanel();
	private JTextField textNo;
	private JComboBox comboBoxCode;

	public static void main(String[] args) {
		try {
			BusinessWindowAlterDialog dialog = new BusinessWindowAlterDialog(managerUI, 0);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public BusinessWindowAlterDialog(ManagerUI managerUI, int serviceWindowId) {
		this.managerUI = managerUI;
		this.serviceWindowId = serviceWindowId;

		// 根据id获取数据库中的windowService信息
		BusinessWindow busWindow = windowService.get(serviceWindowId);

		/**
		 * Create the dialog.
		 */
		setTitle("\u7A97\u53E3\u7BA1\u7406-\u6DFB\u52A0");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			comboBoxCode = new JComboBox();
			comboBoxCode.addItem(busWindow.getCode() + "." + busWindow.getName());
			List<BusinessType> busType = typeServer.findAll();
			for (int i = 0; i < busType.size(); i++) {
				if (!busWindow.getCode().equals(busType.get(i).getCode())
						&& !busWindow.getName().equals(busType.get(i).getName())) {
					comboBoxCode.addItem(busType.get(i).getCode() + "." + busType.get(i).getName());
				}
			}
			comboBoxCode.setBounds(147, 109, 156, 23);
			contentPanel.add(comboBoxCode);
		}
		{
			JLabel lbNo = new JLabel("\u7A97 \u53E3 \u53F7\uFF1A");
			lbNo.setFont(new Font("宋体", Font.PLAIN, 14));
			lbNo.setBounds(60, 38, 84, 23);
			contentPanel.add(lbNo);
		}
		{
			JLabel lbCode = new JLabel("\u4E1A\u52A1\u7C7B\u578B\uFF1A");
			lbCode.setFont(new Font("宋体", Font.PLAIN, 14));
			lbCode.setBounds(60, 109, 84, 23);
			contentPanel.add(lbCode);
		}
		{
			textNo = new JTextField();
			textNo.setText(busWindow.getNo());
			textNo.setBounds(147, 39, 156, 21);
			contentPanel.add(textNo);
			textNo.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("\u4FDD\u5B58");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						BusinessWindow window = new BusinessWindow();
						// 获取数据
						String winNo = textNo.getText();
						String[] busWin = comboBoxCode.getSelectedItem().toString().split("[.]");
						String winCode = busWin[0];
						String winName = busWin[1];
						
						// 校验
						List<BusinessWindow> windows = windowService.findAll();
						for (int i = 0; i < windows.size(); i++) {
							String no=windows.get(i).getNo();
							if (no.equals(winNo)) {
								JOptionPane.showMessageDialog(null, "办理代号不能重复，请重新选择");
								break;
							}
						}
						if (winNo.equals("")) {
							JOptionPane.showMessageDialog(null, "办理代号不能为空");
						} else if (winCode.equals("")) {
							JOptionPane.showMessageDialog(null, "业务代码不能为空");
						} else if (winName.equals("")) {
							JOptionPane.showMessageDialog(null, "业务名称不能为空");
						} else {
							System.out.println(winNo+"  "+busWin[0]+"  "+busWin[1]);
							// 保存数据
							window.setNo(winNo);
							window.setCode(winCode);
							window.setName(winName);
							window.setId(serviceWindowId);
							int ret = windowService.update(window);
							System.out.println(ret);
							if (ret > 0) {
								JOptionPane.showMessageDialog(null, "保存成功");
								// 刷新父窗口的数据
								BusinessWindowAlterDialog.this.managerUI.flushBWData();
								// 关闭对话框
								BusinessWindowAlterDialog.this.dispose();
							} else {
								JOptionPane.showMessageDialog(null, "保存失败");
							}
						}

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("\u53D6\u6D88");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						BusinessWindowAlterDialog.this.dispose();
					}
				});
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 关闭对话框
						BusinessWindowAlterDialog.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	void initVal(BusinessWindow busWin) {

	}
}
